Stop the lease-expiry cron from pinning Neon compute at 100% - #112
Merged
Conversation
The `*/5 * * * *` sweep ran expire() every 5 minutes, 24/7. Neon's serverless compute autosuspends after ~5 minutes idle, so a query on that exact cadence never let it scale to zero: the compute ran continuously (~0.25 CU x 24h x 30d ~= 180 CU-hours/month), blowing past the 100 CU-hour monthly allowance and pinning usage at 100%. The sweep is only a backstop. checkoutTask and listOpenTasks already reclaim lapsed locks lazily on the hot path (reclaimLapsedLocks), so any active use of the pool self-heals a stranded reservation within one request. The cron only does anything when the system is otherwise idle -- which is precisely when we want the compute suspended. Move the backstop to hourly (`0 * * * *`). Idle periods now let Neon autosuspend; the only cost is that a crashed runner's budget reservation in a fully idle system lingers up to ~1h instead of ~5m (the 10-minute lease itself is unchanged). Comments in wrangler.toml, worker.ts, and operations.ts updated to match. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The Neon "100% compute used / at risk of suspension" alert isn't slow queries or user traffic — it's the lease-expiry cron cadence fighting Neon's autosuspend.
wrangler.tomlran the sweep on*/5 * * * *— a DB query every 5 minutes, 24/7. Neon's serverless compute autosuspends after ~5 minutes of inactivity, so a query on exactly that interval means the compute never scales to zero. It runs continuously:Fix
Move the sweep to hourly (
0 * * * *). Safe because the cron is only a backstop:checkoutTaskandlistOpenTasksalready reclaim lapsed locks lazily (reclaimLapsedLocks) on the hot path, so any active use of the pool self-heals a stranded reservation within a single request.Idle periods now let Neon autosuspend (cron wake time drops from ~24h/day to ~2h/day, well under the allowance with headroom for real traffic). The only tradeoff: in a fully idle system, a crashed runner's budget reservation frees up in up to ~1h instead of ~5m. The 10-minute lease itself is unchanged, and the moment anyone touches the pool it's reclaimed immediately.
Changes
wrangler.toml— cron*/5 * * * *→0 * * * *, with a comment explaining the autosuspend interaction.src/worker.ts/src/operations.ts— updated the "every 5 minutes" / "5-minute cron sweep" comments to match.Verification
npm run typecheck— passnpm run lint— passlocalhost:5433(the suite truncates tables and requires a local DB). No runtime logic changed and no test references the cron schedule, so nothing in the suite exercises this change.Note for reviewer
This assumes Neon's autosuspend is at its default (~300s idle). If that window has been raised, the cron cadence should be retuned so the two don't fight.
🤖 Generated with Claude Code
Generated by Claude Code